package com.yidu.ruifeng_logistics.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yidu.ruifeng_logistics.entity.LogisticsArea;
import com.yidu.ruifeng_logistics.entity.LogisticsStorage;
import com.yidu.ruifeng_logistics.service.LogisticsAreaService;
import com.yidu.ruifeng_logistics.service.LogisticsStorageService;
import com.yidu.ruifeng_logistics.utils.BaseMessage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (LogisticsStorage)表控制层
 *
 * @author 肖楚科
 * @since 2021-03-09 14:29:23
 */
@Api(description = "仓库接口")
@RestController
@RequestMapping("logisticsStorage")
public class LogisticsStorageController {
    /**
     * 服务对象
     */
    @Resource
    private LogisticsStorageService logisticsStorageService;

    @Resource
    private LogisticsAreaService logisticsAreaService;

    /**
     * 通过主键查询单条数据
     * 作为修改时赋值使用
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation(value = "根据id查询仓库",notes = "返回仓库对象")
    @PostMapping("/showUpdate")
    public BaseMessage selectOne(Integer id) {
        return BaseMessage.success(this.logisticsStorageService.queryById(id));
    }

    /**
     * 根据条件查询仓库
     * @param logisticsStorage 仓库对象
     * @return 信息对象
     */
    @ApiOperation(value = "根据条件查询全部仓库",notes = "返回状态信息")
    @GetMapping("/queryAll")
    public  Map<String, Object> queryAll(LogisticsStorage logisticsStorage, int page, int limit,String keyWord){
        //将参数传入到分页助手
        PageHelper.startPage(page,limit);
        logisticsStorage.setKeyWord(keyWord);
        List<LogisticsStorage> list = logisticsStorageService.queryAll(logisticsStorage);
        Map<String, Object> map = new HashMap<>();

        PageInfo<LogisticsStorage> pageInfo = new PageInfo<>(list);

        map.put("code",0);
        map.put("msg","查询成功");
        map.put("data",pageInfo.getList());//可以直接写list
        map.put("count",pageInfo.getTotal()); //总行数
        return map;
    }

    /**
     * 根据id删除仓库
     * @param id id参数
     * @return 状态信息
     */
    @ApiOperation(value = "根据id删除仓库",notes = "返回状态信息")
    @PostMapping("/delete")
    public BaseMessage delete( int id){
        return logisticsStorageService.deleteById(id)?BaseMessage.success():BaseMessage.fail();
    }

    /**
     * 批量删除方法
     * @param ids id拼接字符串
     * @return 状态信息
     */
    @ApiOperation(value = "批量删除仓库",notes = "返回状态信息")
    @PostMapping("/deleteAll")
    public BaseMessage deleteAll( String ids){
        return logisticsStorageService.deleteAll(ids)>0?BaseMessage.success():BaseMessage.fail();
    }

    /**
     * 根据id新增或者修改仓库信息
     * @param logisticsStorage 仓库对象
     * @return 状态信息
     */
    @ApiOperation(value = "增加或修改仓库",notes = "返回状态信息")
    @PostMapping("/addOrUpdate")
    public BaseMessage addOrUpdate(LogisticsStorage logisticsStorage){
        if (StringUtils.isEmpty(logisticsStorage.getStorageId())){
            logisticsStorageService.insert(logisticsStorage);
        }else {
            logisticsStorageService.update(logisticsStorage);
        }
        return BaseMessage.success();
    }

}
